package com.truthbean.jeetb.api.service.oschina;

import com.alibaba.fastjson.JSON;
import com.truthbean.jeetb.api.config.oschina.Config;
import com.truthbean.jeetb.utils.NetWorkUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import org.json.XML;

import javax.enterprise.inject.Alternative;

/**
 * Created by TruthBean on 2016-10-09 14:47.
 */
public class NewsService {
    private Logger logger = LogManager.getLogger(getClass());

    public String getNewsList(int catalog) {
        String xmlStr = NetWorkUtils.doHttpGet(Config.NEWS_LIST + "?catalog=" + catalog);
        JSONObject json = XML.toJSONObject(xmlStr);
        logger.info(json);
//        com.alibaba.fastjson.JSONObject jsonObj = JSON.parseObject(json.toString());
//        if (jsonObj.containsKey("oschina")) {
//            Object oschina = jsonObj.get("oschina");
//            logger.info(oschina);
//            if (oschina instanceof String){
//                logger.info(oschina);
//            }
//        }
        return json.toString();
    }

    public String getNewsDetailById(int id) {
        String xmlStr = NetWorkUtils.doHttpGet(Config.NEWS_DETAIL + "?id=" + id);
        JSONObject json = XML.toJSONObject(xmlStr);
        logger.info(json);
        //com.alibaba.fastjson.JSONObject jsonObj = JSON.parseObject(json.toString());
//        if (jsonObj.containsKey("oschina")) {
//            Object oschina = jsonObj.get("oschina");
//            logger.info(oschina);
//            if (oschina instanceof String){
//                logger.info(oschina);
//            }
//        }
        return json.toString();
    }
}
